//
//  XLTabBarView.m
//  MSCustomTabBarController
//
//  Created by 于鹏 on 2017/8/9.
//  Copyright © 2017年 MrSong. All rights reserved.
//

#import "XLTabBarView.h"
#import "XLTabBarButton.h"
#import "XLMainDefine.h"

#define kTabBarTag 1000
@interface XLTabBarView ()

@property (nonatomic, strong) IBOutletCollection(UIView) NSArray *items;

@property (nonatomic, assign)NSInteger selectIndex;//记录当前选中button

@end

@implementation XLTabBarView

#pragma mark - LifeCycle

+ (instancetype)tabBarView {
    return [[[NSBundle bundleForClass:[self class]] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectIndex = 0;
    
        NSArray *normalImgArr = @[
                                  @"tabbar_current_n.png",
                                  @"tabbar_history_n.png",
                                  @"tabbar_new_n.png"
                                  ];
        NSArray *selectedImgArr = @[
                                    @"tabbar_current_s.png",
                                    @"tabbar_history_s.png",
                                    @"tabbar_new_s.png"
                                    ];
       UIColor *normalColor = [UIColor colorWithRed:32/255.0 green:122/255.0 blue:193/255.0 alpha:1.0];
    for (int i = 0; i < self.items.count; i++) {
        XLTabBarButton *button = self.items[i];
        
        [button setTitleColor:normalColor forState:UIControlStateNormal];
        [button setTitleColor:normalColor forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        [button setImage:XLImage(normalImgArr[i]) forState:UIControlStateNormal];
        [button setImage:XLImage(selectedImgArr[i]) forState:UIControlStateDisabled];
        [button setImage:XLImage(normalImgArr[i]) forState:UIControlStateHighlighted];

    }
    
    
    
    
}

#pragma mark - Action
- (IBAction)selectItem:(UIButton *)sender {
    self.selectIndex = sender.tag;
    if (self.delegate && [self.delegate respondsToSelector:@selector(xlTabBarView:didSelectItemAtIndex:)]) {
        [self.delegate xlTabBarView:self didSelectItemAtIndex:sender.tag];
    }
}

#pragma mark - Setter 

- (void)setSelectIndex:(NSInteger)selectIndex {
    // 先把上次选择的item设置为可用
    UIButton *lastBtn = _items[_selectIndex];
    lastBtn.enabled = YES;
    // 再把这次选择的item设置为不可用
    UIButton *item = _items[selectIndex];
    item.enabled = NO;
    _selectIndex = selectIndex;
}

@end
